﻿using Sunny.UI;
using System;
using System.IO.Pipes;
using System.IO;
using System.Threading;
using CoreShop.Model.FromBody;
using CoreShop.Library;
using CoreShop.Model;
using System.Collections.Generic;
using CoreShop.Services.Services.Report;
using CoreShop.App.Units;
using System.Windows.Forms;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;

namespace CoreShop.App
{
    public partial class fpgGiftSum : UIPage
    {
        public int mPageIndex = 0;
        public fpgGiftSum()
        {
            InitializeComponent();
        }

        private void fpgGiftSum_ReceiveParams(object sender, UIPageParamsArgs e)
        {
            mPageIndex = 1;
            var rptRequest = e.Value as PosReportRequest;
            if (rptRequest.type == (int)GlobalEnumVars.ReportRequest.Query)
            {
                LoadData(rptRequest);
            }
            else
            {
                ExportData();
            }
        }

        private void ExportData()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.Title = "Export Excel File To";
            saveFileDialog.ShowDialog();
            string fileName = saveFileDialog.FileName;
            if (fileName == "") return;
            Dictionary<string, DataGridView> gridViews = new Dictionary<string, DataGridView>();
            gridViews.Add("销售汇总", dgvDotGiveRefundList);
            if (ExportUtil.NPOIExportForDataGridview(gridViews, fileName))
            {
                MessageBox.Show("导出Excel成功！");
            }
        }


        /// <summary>
        ///销售统计
        /// </summary>
        private void LoadData(PosReportRequest posReport)
        {
            int count = 0;
            List<Reportscorecmsfoodordering> lstSource = new corereportServices()
                .GetCorecmsfoodorderings(ref count, mPageIndex, btnPg3.PageSize , posReport.starTime, 
                posReport.endTime , this.tab3_txtName.Text, this.tab3_txtType.Text);

            btnPg3.TotalCount = count;
            this.dgvDotGiveRefundList.AutoGenerateColumns = false;
            this.dgvDotGiveRefundList.DataSource = lstSource;
            //设置统计绑定的表格
            var ss = lstSource.Sum(p => p.quantity);
            dgvFooter.DataGridView = dgvDotGiveRefundList;
            dgvFooter["foodTypeName"] = "合计：";
            dgvFooter["quantity"] = CommFunc.ConvertToString(lstSource.Sum(p=>p.quantity));
            dgvFooter["amount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.amount));
            dgvFooter["refQuantity"] = CommFunc.ConvertToString(lstSource.Sum(p => p.refQuantity));
            dgvFooter["refAmount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.refAmount));
            dgvFooter["giveQuantity"] = CommFunc.ConvertToString(lstSource.Sum(p => p.giveQuantity));
            dgvFooter["giveAmount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.giveAmount));
            dgvFooter["saleQuantity"] = CommFunc.ConvertToString(lstSource.Sum(p => p.saleQuantity));
            dgvFooter["saleAmount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.saleAmount));
            dgvFooter["reduceAmount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.reduceAmount));
        }
    }
}